// 获取字典
import { useState } from 'react';
import { useDeepCompareEffect } from 'react-use';
import request from '@/utils/request';

export const useDictionary = params => {
    const [loading, setLoading] = useState(false);
    const [dictionary, setDictionary] = useState([]);

    useDeepCompareEffect(() => {
        const fetchDictionary = async () => {
            if (!params.code && !params.codes) return;
            setLoading(true);
            const response = await request('/api/dict/v1/getList', {
                data: params,
                method: 'POST'
            });

            if (response.code === 10000) {
                setDictionary(response.data);
            }
            setLoading(false);
        };

        fetchDictionary();
    }, [params]);

    return {
        loading,
        dictionary
    };
};
